﻿// -----------------------------------------------------------------------
// <copyright file="CountryLanguageRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CountryLanguageRepository : AbstractRepository<CountryLanguage>
    {
        public CountryLanguageRepository(ContextHelper contextHelper)
        {
            base._contextHelper = contextHelper;
            base._context = _contextHelper.GetContext();
        }

        public override IQueryable<CountryLanguage> GetAll()
        {
            var result = (from x in base._context.CountryLanguages
                          select new CountryLanguage
                          {
                              CountryId = x.CountryId,
                              LanguageId = x.LanguageId,
                              IsSpoken = x.IsSpoken,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(CountryLanguage entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.CountryLanguage entityToAdd = new Context.CountryLanguage
            {
                CountryId = entity.CountryId,
                LanguageId = entity.LanguageId,
                IsSpoken = entity.IsSpoken,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };

            base._context.CountryLanguages.InsertOnSubmit(entityToAdd);
        }

        public override void Update(CountryLanguage entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.CountryLanguage entityToUpdate = base._context.CountryLanguages.Single(x => x.IsSpoken.Equals(entity.IsSpoken));

            entityToUpdate.CountryId = entity.CountryId;
            entityToUpdate.LanguageId = entity.LanguageId;
            entityToUpdate.IsSpoken = entity.IsSpoken;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(CountryLanguage entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.CountryLanguage entityToDelete = base._context.CountryLanguages.Single(x => x.IsSpoken.Equals(entity.IsSpoken));

            base._context.CountryLanguages.DeleteOnSubmit(entityToDelete);
        }
    }
}
